<?php
$loader->requireOnce('includes/Utils.class.php');
$loader->requireOnce('ordo/EmployeeType.class.php');


class Employee extends ORDataObject {
	var $person_id			= '';
	var $name				= '';
	var $alias				= '';
	var $hire_date			= '';
	var $employee_type_id	= '';
	var $disabled			= '';
	var $department_id		= '';
	var $diploma_id			= '';
	var $contract_type		= '';
	var $mapped				= '';

	var $email				= '';
	var $phone_number		= '';
	var $date_of_birth		= '';
	var $gender				= '';
	var $notes				= '';

	var $_table				= 'employee';
	var $_key				= 'person_id';
	var $_internalName		= 'Employee';

	const DOCTOR_ID			= 1;
	const NURSE_ID			= 2;
	const INTERMEDIARY_ID	= 6;

	function Employee() {
		parent::ORDataObject();
	}

	function getHiredDate(){
        return $this->getLocalizedDate('hired_date');
	}

	public function getEmployeeForAutoComplete($needle, $userType = array(), $departmentId = false) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'	=> "e.person_id, e.name, e.alias",
            'from'	=> "$this->_table e",
            'where'	=> "(e.name LIKE '%$needle%' OR e.alias LIKE '%$needle%')"
		);
		if ($departmentId !== false) {
		    $sql['where'] .= " AND e.department_id = $departmentId";
		}
        if (!empty($userType)) {
        	$sql['where'] .= ' AND e.employee_type_id IN (' . implode(',', $userType) . ')';
        }
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
	}

	public function getCashierList($toArrayKeyValue = false) {
		return $this->getUserList(array(EmployeeType::CASHIER_ID), $toArrayKeyValue);
	}

	public function getDoctorList($toArrayKeyValue = false) {
		return $this->getUserList(array(EmployeeType::DOCTOR_ID, EmployeeType::TECHNICAN_ID), $toArrayKeyValue);
	}

	public function getUserList($userType = '', $toArrayKeyValue = false) {
		$ds =& new Datasource_sql();
        $sql = array(
            'cols'	=> "e.person_id, e.name",
            'from'	=> "$this->_table e
						INNER JOIN user u ON e.person_id = u.person_id"
		);
		$sql['where'] = "u.enable = 1";
        if (!empty($userType)) {
        	if (is_array($userType)) {
	        	$types	=	implode(",", $userType);
	        	$sql['where']	.=	" AND e.employee_type_id IN ($types)";
        	}
        	else {
        		$sql['where']	.=	" AND e.employee_type_id = $userType";
        	}
        }

        $ds->setup($this->_db, $sql);
		if ($toArrayKeyValue) {
			return $ds->toArray('person_id', 'name');
		} else {
			return $ds->toArray();
		}
	}

	public function getNoCoefficientEmployeeList($typeId){
		$field		=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');

		$ds		=	& new Datasource_sql();
		$sql	=	array(
			'cols'	=>	"e.person_id, e.name, t.$field as typeName, r.$field as roomName",
			'from'	=>	"employee e
						LEFT JOIN employee_type t ON e.employee_type_id=t.employee_type_id
						LEFT JOIN user u ON u.person_id=e.person_id
						LEFT JOIN room r ON u.room_id = r.room_id"
		);
		$sql['where'] = "e.person_id NOT IN (SELECT person_id FROM fee_coefficient)";
		if($typeId != false){
			$sql['where'] .= " AND t.employee_type_id=$typeId";
		}

		$ds->setup($this->_db,$sql);
        return $ds->toArray();
	}

	public function getEmployeeToAdd($employeeTypeId){
		$sql = array(
				'cols'	=> "e.person_id, e.name ",
			   	'from'	=> "$this->_table e
			   				INNER JOIN user u ON (e.person_id = u.person_id)",
			   	'where'	=> "e.employee_type_id = $employeeTypeId AND u.disabled = 'no'"
			   	);
		$ds = new Datasource_sql();
	    $ds->setup($this->_db, $sql);
	    return $ds->toArray();
	}

	public function getEmployeeSuggestion($part) {
		$sql = "SELECT name FROM employee
				WHERE name LIKE '%$part%' AND (employee_type_id = " . Employee::DOCTOR_ID
				. " OR employee_type_id = " . Employee::NURSE_ID
				. " OR employee_type_id = " . Employee::INTERMEDIARY_ID
				. ") LIMIT 10";

		return $this->_execute($sql);
	}


	public function getEmployeeOfDepartment($departmentId = 0 , $employeeType=false, $toArrayKeyValue=false) {
	    $ds = new Datasource_sql();
        $sql = array(
			'cols'	=> "e.person_id, e.name",
			'from'	=> "$this->_table e",
			'where'	=> "e.enable = 1 "
        );
        // If $employeeType=false, get all employee of the department
        if ($departmentId != 0) {
            $sql['where'] .= ' AND department_id = ' . $departmentId;

        }
        if ($employeeType) {
        	if(is_array($employeeType) && count($employeeType) > 0) {
                $sql['where'] .= ' AND employee_type_id IN (' . implode(',', $employeeType) . ')';
        	} else {
                $sql['where'] .= " AND employee_type_id = '$employeeType'";
        	}
        }

        $ds->setup($this->_db, $sql);
        if ($toArrayKeyValue) {
            return $ds->toArray('person_id', 'name');
        } else {
            return $ds->toArray();
        }
	}
	/*
	 * For HR Module
	 */
	public function getEmployeeList($deptId = 0, $typeId = 0, $mapped = 'yes', $toArrayIdName = false) {
		$field		=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array(
			'cols'	=> "e.person_id, e.name, t.$field as type, d.$field as dept",
			'from'	=> "$this->_table e
						INNER JOIN employee_type t ON e.employee_type_id = t.employee_type_id
						INNER JOIN department d ON e.department_id = d.department_id
						",
			'where'	=> "true"
        );
        if ($deptId > 0) {
        	$sql['where'] .= " AND e.department_id = $deptId";
        }
        if ($typeId > 0) {
        	$sql['where'] .= " AND e.employee_type_id = $typeId";
        }
        if ($mapped != 'all') {
        	$sql['where'] .= " AND e.mapped = '$mapped'";
        }
        $ds->setup($this->_db, $sql);
        if ($toArrayIdName) {
        	return $ds->toArray('person_id', 'name');
        }
        return $ds->toArray();
	}

	/*
	 * Temporarily put here, should move to Diploma Ordo
	 */
	public function getDiplomaList($parentId = 0, $toArrayKeyValue = false) {
		$field		=	L10nHelper::getLocale();
		$field		=	L10nHelper::changeField($field, "name");
		$ds = new Datasource_sql();
		$sql = array(
			'cols'	=> "d1.diploma_id, CONCAT(d1.$field, ' (', d2.$field, ')') AS name",
			'from'	=> "diploma d1 INNER JOIN diploma d2 ON d1.parent_id = d2.diploma_id",
			'where'	=> "d1.parent_id > 0"
        );
        if ($parentId > 0) {
        	$sql['where'] .= " AND d1.parent_id = $parentId";
        }
        $ds->setup($this->_db, $sql);
	 	if ($toArrayKeyValue) {
            return $ds->toArray('diploma_id', 'name');
        } else {
            return $ds->toArray();
        }
	}

	public function getName() {
		return $this->name;
	}
}
?>
